package com.cloudcc.mobile.adapter;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.FeedFile;
import com.cloudcc.mobile.entity.MyChatter;
import com.cloudcc.mobile.entity.addmicropost.Polls;
import com.cloudcc.mobile.entity.comment.Comments;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.DongHuaUtils;
import com.cloudcc.mobile.util.ImageUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.UserUtils;
import com.cloudcc.mobile.view.activity.MyWebActivity;
import com.cloudcc.mobile.view.activity.NewUserInfoActivity;
import com.cloudcc.mobile.view.activity.UserInfoActivity;
import com.cloudcc.mobile.view.dynamic.RelevantActivtiy;
import com.cloudcc.mobile.view.web.IWebView;
import com.cloudcc.mobile.weight.GridViewForListView;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
 * 微贴动态适配器 4.6 抛弃 {@link DynamicAdapter}
 * 
 * @author seven
 * 
 */
@Deprecated
public class ChatIndexAdapter extends BaseAdapter {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private Context context;
	private List<MyChatter> list;
	private OnItemListener onItemListener;

	// 构造方法
	public ChatIndexAdapter(Context context) {
		this.context = context;
	}

	public void setOnItemtListener(OnItemListener onItemListener) {
		this.onItemListener = onItemListener;
		this.notifyDataSetChanged();
	}

	// 设置更新界面
	public void changeData(List<MyChatter> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public MyChatter getItem(int postion) {
		return list.get(postion);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.home_llistview_item2, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.nicheng = (TextView) convertView.findViewById(R.id.nicheng);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.wenben = (TextView) convertView.findViewById(R.id.wbnr);
			holder.image = (ImageView) convertView.findViewById(R.id.imageaaa);
			holder.download = (ImageView) convertView
					.findViewById(R.id.download);
			holder.lianjiename = (TextView) convertView
					.findViewById(R.id.lianjielanse);
			holder.lianjieimg = (ImageView) convertView
					.findViewById(R.id.lianjieimg);
			holder.downwenjian = (TextView) convertView
					.findViewById(R.id.downwenjian);
			holder.countxh = (TextView) convertView.findViewById(R.id.countxh);
			holder.address = (TextView) convertView.findViewById(R.id.address);
			holder.countly = (TextView) convertView
					.findViewById(R.id.tvpingluna);
			holder.comment = (ImageView) convertView.findViewById(R.id.comment);
			holder.praised = (ImageView) convertView.findViewById(R.id.praied);
			holder.favourite = (ImageView) convertView
					.findViewById(R.id.favourite);
			holder.delete = (ImageView) convertView.findViewById(R.id.delete);
			holder.targetimg = (ImageView) convertView
					.findViewById(R.id.targetimg);
			holder.gv = (GridViewForListView) convertView.findViewById(R.id.gv);
			holder.counttp = (TextView) convertView.findViewById(R.id.countp);
			holder.bttoupiao = (Button) convertView
					.findViewById(R.id.bttoupiao);
			holder.zhengti = (RelativeLayout) convertView
					.findViewById(R.id.zhengti);
			holder.shoucang = (RelativeLayout) convertView
					.findViewById(R.id.shoucang);
			holder.xihuan = (RelativeLayout) convertView
					.findViewById(R.id.xihuan);
			holder.shanchuaa = (RelativeLayout) convertView
					.findViewById(R.id.shanchuaq);
			holder.pinglun = (RelativeLayout) convertView
					.findViewById(R.id.pinglun);
			holder.downloadpic = (RelativeLayout) convertView
					.findViewById(R.id.downloadpic);
			holder.lianjieurl = (RelativeLayout) convertView
					.findViewById(R.id.lianjieurlll);
			holder.aboutpic = (RelativeLayout) convertView
					.findViewById(R.id.aboutpic);
			holder.abouttoupiao = (LinearLayout) convertView
					.findViewById(R.id.abouttoupiao);
			holder.abcapoll = (LinearLayout) convertView
					.findViewById(R.id.abcapoll);
			holder.target = (LinearLayout) convertView
					.findViewById(R.id.target);
			holder.aboutaddress = (RelativeLayout) convertView
					.findViewById(R.id.aboutaddresss);
			holder.targetIdname = (TextView) convertView
					.findViewById(R.id.targetIdname);
			holder.targetIdlx = (TextView) convertView
					.findViewById(R.id.targetIdlx);
			holder.event = (LinearLayout) convertView.findViewById(R.id.event);
			holder.eventimg = (ImageView) convertView
					.findViewById(R.id.eventimg);
			holder.eventIdname = (TextView) convertView
					.findViewById(R.id.eventidname);
			holder.below_address = (TextView) convertView
					.findViewById(R.id.below_address);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final MyChatter myChatter = getItem(position);
		// 留言赋值
		try {
			List<Comments> list1 = myChatter.getComments();
			if (list1 == null || list1.size() == 0) {
				holder.countly.setText("");
			} else {
				holder.countly.setText("" + list1.size());
			}
		} catch (Exception e) {
			Tools.handle(e);
		}

		// 喜欢赋值 如果是0 就设置为 空
		if (myChatter.getPraisenum() == null
				|| myChatter.getPraisenum().equals("")) {
			holder.countxh.setText("");
		} else {
			holder.countxh.setText(myChatter.getPraisenum());
		}
		// 赞
		if (myChatter.isIspraised()) {
			holder.praised.setImageResource(R.drawable.chatter_praisedact);
			if (!AppContext.iszan.equals("")) {
				if (AppContext.iszan.equals(myChatter.getId())) {
					DongHuaUtils.startScaleAnimationJavaCode(holder.praised);
					AppContext.iszan = "";
				}
			}
		} else {
			holder.praised.setImageResource(R.drawable.chatter_praised);
			if (!AppContext.iszan.equals("")) {
				if (AppContext.iszan.equals(myChatter.getId())) {
					DongHuaUtils.startScaleAnimationJavaCode(holder.praised);
					AppContext.iszan = "";
				}
			}
		}
		// 收藏
		if (myChatter.isIsfavorited()) {
			holder.favourite.setImageResource(R.drawable.chatter_favouriteact);
			if (!AppContext.shoucang.equals("")) {
				if (AppContext.shoucang.equals(myChatter.getId())) {
					DongHuaUtils.startScaleAnimationJavaCode(holder.favourite);
					AppContext.shoucang = "";
				}
			}
		} else {
			holder.favourite.setImageResource(R.drawable.chatter_favourite);
			if (!AppContext.shoucang.equals("")) {
				if (AppContext.shoucang.equals(myChatter.getId())) {
					DongHuaUtils.startScaleAnimationJavaCode(holder.favourite);
					AppContext.shoucang = "";
				}
			}
		}
		// //头像
		String url = UrlTools.getTopImage(myChatter.getAuthorId());
		holder.icon.postInvalidate();
		UserUtils.setLogoRound(url, holder.icon,
				UserUtils.getBackGroudColorByUserIdHash(
						myChatter.getAuthorId(), context), UserUtils
						.getNickRowName(myChatter.getAuthorIdname()), context);
		/*
		 * if (myChatter.isPhoto()) { String url =
		 * UrlTools.getTopImage(myChatter.getAuthorId()); String icon =
		 * myChatter.getAuthorId() + ".png"; String path =
		 * Tools.getImagePath("/CloudCC/UserIcon/"); if (!new
		 * File(path).exists()) { new File(path).mkdirs(); } String iconpath =
		 * Tools.getImagePath("/CloudCC/UserIcon/") + "/" + icon; // File
		 * file=new File(iconpath); // if(file.exists()){ //
		 * ImageLoader.getInstance
		 * ().displayImage("file://"+iconpath,holder.icon,
		 * Constants.chater_index_icon_optionst); // }else{ //
		 * ImageLoadingListener animateFirstListener = new //
		 * AnimateFirstDisplayListener(icon,"/CloudCC/UserIcon/"); //
		 * ImageLoader.getInstance().displayImage(url, holder.icon, //
		 * Constants.chater_index_icon_optionst,animateFirstListener); //
		 * ImageLoader.getInstance().displayImage(url, holder.icon, //
		 * Constants.chater_index_icon_optionst); // }
		 * holder.icon.postInvalidate(); UserUtils.setContactUserLogo(url,
		 * holder.icon,
		 * UserUtils.getBackGroudColorByUserIdHash(myChatter.getAuthorId(),
		 * context), UserUtils.getNickRowName(myChatter.getAuthorIdname()),
		 * context); } else {
		 * holder.icon.setImageResource(R.drawable.person110); }
		 */
		// 昵称
		if (Tools.isNull(myChatter.getAuthorIdname())) {
			if ("en".equals(mEns)) {
				holder.nicheng.setText("No time");
				}else{
					holder.nicheng.setText("暂无");
			}
			
		} else {
			holder.nicheng.setText(myChatter.getAuthorIdname());
			holder.nicheng.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
		}
		// 时间
		if (Tools.isNull(myChatter.getCreateDate())) {
			if ("en".equals(mEns)) {
				holder.nicheng.setText("No time");
				}else{
					holder.nicheng.setText("暂无");
			}
		} else {
			String a = myChatter.getCreateDate().substring(0, 5);
			String b = myChatter.getCreateDate().substring(5);
			if ("en".equals(mEns)) {
				if (a.equals("今天,上午")) {
					holder.time.setText(b + " AM");
				} else if (a.equals("Nowadays,in the afternoon")) {
					holder.time.setText(b + " PM");
				} else if (a.equals("yesterday,morning")) {
					holder.time.setText("yesterday " + b + " AM");
				} else if (a.equals("yesterday,in the afternoon")) {
					holder.time.setText("yesterday " + b + " PM");
				} else if (a.equals("The day before yesterday,morning")) {
					holder.time.setText("The day before yesterday " + b + " AM");
				} else if (a.equals("The day before yesterday,in the afternoon")) {
					holder.time.setText("The day before yesterday " + b + " PM");
				} else {
					holder.time.setText(myChatter.getCreateDate());
				}
//				----------------------------------------------------
				}else{
					if (a.equals("今天,上午")) {
						holder.time.setText(b + " AM");
					} else if (a.equals("今天,下午")) {
						holder.time.setText(b + " PM");
					} else if (a.equals("昨天,上午")) {
						holder.time.setText("昨天 " + b + " AM");
					} else if (a.equals("昨天,下午")) {
						holder.time.setText("昨天 " + b + " PM");
					} else if (a.equals("前天,上午")) {
						holder.time.setText("前天 " + b + " AM");
					} else if (a.equals("前天,下午")) {
						holder.time.setText("前天 " + b + " PM");
					} else {
						holder.time.setText(myChatter.getCreateDate());
					}
			}
			
		}
		String body;
		// 正文
		if (null == (myChatter.getBody())) {
			body = "";
		} else {

			body = Html.fromHtml(myChatter.getBody()).toString();
		}
		holder.wenben.setText("");
		if (myChatter.getFeedType().equals("L")) {
			isContiaTarget(holder, myChatter);
			if (body.length() > 100) {
				if ("en".equals(mEns)) {
					setBody("Share links " + myChatter.getBody().substring(0, 100)
							+ ".....", holder.wenben);
					}else{
						setBody("分享链接  " + myChatter.getBody().substring(0, 100)
								+ ".....", holder.wenben);
				}
				
			} else if (body.length() == 0) {
				if ("en".equals(mEns)) {
					setBody("Share links  ", holder.wenben);
					}else{
						setBody("分享链接  ", holder.wenben);
				}
			
			} else {
				if ("en".equals(mEns)) {
					setBody("Share links  " + myChatter.getBody(), holder.wenben);
					}else{
						setBody("分享链接  " + myChatter.getBody(), holder.wenben);
				}
				
			}

		} else if (myChatter.getFeedType().equals("D")) {
			isContiaTarget(holder, myChatter);
			if (myChatter.getFeedFile() != null) {
				List<FeedFile> ff = myChatter.getFeedFile();
				if (ff.size() > 0) {
					FeedFile f = ff.get(0);
					String type = "";
					if (f != null) {
						type = f.getContentType();
					}
					if (type.equals("jpg") || type.equals("JPG")
							|| type.equals("png") || type.equals("PNG")
							|| type.equals("jpeg") || type.equals("bmp")
							|| type.equals("gif")) {
						if (body.length() > 100) {
							if ("en".equals(mEns)) {
								setBody("share pictures  "
										+ myChatter.getBody().substring(0, 100)
										+ ".....", holder.wenben);
								}else{
									setBody("分享图片  "
											+ myChatter.getBody().substring(0, 100)
											+ ".....", holder.wenben);
							}
							
						} else {
							if ("en".equals(mEns)) {
								setBody("share pictures  " + myChatter.getBody(),
										holder.wenben);
								}else{
									setBody("分享图片  " + myChatter.getBody(),
											holder.wenben);
							}
							
						}
					} else {
						if (body.length() > 100) {
							if ("en".equals(mEns)) {
								setBody("share file  "
										+ myChatter.getBody().substring(0, 100)
										+ "....." + ".....", holder.wenben);
								}else{
									setBody("分享文件  "
											+ myChatter.getBody().substring(0, 100)
											+ "....." + ".....", holder.wenben);
							}
							
						} else {
							if ("en".equals(mEns)) {
								setBody("share file  " + myChatter.getBody(),
										holder.wenben);
								}else{
									setBody("分享文件  " + myChatter.getBody(),
											holder.wenben);
							}
							
						}
					}
				}
			}
		} else if (myChatter.getFeedType().equals("V")) {//
			isContiaTarget(holder, myChatter);
			String tick = "";
			if ("en".equals(mEns)) {
				tick = "vote";
				}else{
					tick = "投票";
			}
			
			if (body.length() > 100) {
				
				setBody(tick + myChatter.getBody().substring(0, 100)
						+ "......", holder.wenben);
			} else {
				setBody(tick + myChatter.getBody(), holder.wenben);
			}
		} else if (myChatter.getFeedType().equals("G")) {
			isContiaTarget(holder, myChatter);
			if (body.length() > 100) {
				setBody(myChatter.getBody().substring(0, 100) + ".....",
						holder.wenben);
			} else {
				setBody(myChatter.getBody(), holder.wenben);
			}
		} else {
			isContiaTarget(holder, myChatter);
			if (body.length() > 100) {
				setBody(myChatter.getBody().substring(0, 100) + ".....",
						holder.wenben);
			} else {
				setBody(myChatter.getBody(), holder.wenben);
			}
		}
		// 投票
		if (myChatter.getFeedType().equals("V")) {
			holder.abcapoll.removeAllViews();
			holder.abouttoupiao.setVisibility(View.VISIBLE);
			List<Polls> list = myChatter.getPolls();
			if (list != null) {
				Tools.i("getFeedType", "list.size" + list.size());
				int count = 0;
				for (int i = 0; i < list.size(); i++) {
					String[] a = list.get(i).getOptionUsers();
					if (a != null) {
						count = count + a.length;
					}
					View v = LayoutInflater.from(context).inflate(
							R.layout.poll_item2, null);
					TextView tv = (TextView) v.findViewById(R.id.textp);
					tv.setText(i + 1 + " " + list.get(i).getOption());
					// tv.setTextSize(context.getResources().getDimension(R.dimen.radiogroup_textsize));
					holder.abcapoll.addView(v);
				}
				if ("en".equals(mEns)) {
					holder.counttp.setText(count + "ticket");
					}else{
						holder.counttp.setText(count + "票");
				}
				
			}
		} else {
			holder.abouttoupiao.setVisibility(View.GONE);
		}

		// 链接
		if (myChatter.getFeedType().equals("L")) {
			holder.lianjieurl.setVisibility(View.VISIBLE);
			holder.lianjiename.setVisibility(View.VISIBLE);
			holder.lianjiename.setText(myChatter.getLinkName());
			holder.lianjieurl.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, MyWebActivity.class);
					intent.putExtra(IWebView.KEY_URL, myChatter.getLinkValue());
					if ("en".equals(mEns)) {
						intent.putExtra(IWebView.KEY_NAME, StringUtils
								.isBlank(myChatter.getLinkName()) ? "Share links"
								: "Share links-" + myChatter.getLinkName());
						}else{
							intent.putExtra(IWebView.KEY_NAME, StringUtils
									.isBlank(myChatter.getLinkName()) ? "分享链接"
									: "分享链接-" + myChatter.getLinkName());
					}
					
					context.startActivity(intent);
				}
			});
		} else {
			holder.lianjieurl.setVisibility(View.GONE);
			holder.lianjiename.setVisibility(View.GONE);
		}
		// 地址赋值
		String aaa = myChatter.getAddress();
		if (aaa == null || aaa.equals(" ") || aaa.equals("")
				|| aaa.equals("null")) {
			if ("en".equals(mEns)) {
				holder.address.setText("No address");
				}else{
					holder.address.setText("暂无地址");
			}
			
			holder.aboutaddress.setVisibility(View.GONE);
		} else {
			holder.aboutaddress.setVisibility(View.VISIBLE);
			holder.address.setText(myChatter.getAddress());
		}
		/** * 图 */
		if (myChatter.getFeedType().equals("D")) {
			holder.aboutpic.setVisibility(View.VISIBLE);
			if (myChatter.getFeedFile() != null) {
				List<FeedFile> l = myChatter.getFeedFile();
				LayoutParams para;
				para = holder.gv.getLayoutParams();
				if (l.size() == 1) {
					holder.gv.setNumColumns(1);
					para.height = LayoutParams.WRAP_CONTENT;
					para.width = LayoutParams.WRAP_CONTENT;
				} else if (l.size() == 4) {
					holder.gv.setNumColumns(2);
					para.height = AppContext.screenW / 2;
					para.width = AppContext.screenW / 2;
				} else {
					holder.gv.setNumColumns(3);
					para.height = AppContext.screenW * 3 / 4;
					para.width = AppContext.screenW * 3 / 4;
				}
				holder.gv.setLayoutParams(para);
				if (l.size() > 0) {
					String type = myChatter.getFeedFile().get(0)
							.getContentType();
					holder.downwenjian.setText(myChatter.getFeedFile().get(0)
							.getFilename()
							+ "."
							+ type
							+ "("
							+ myChatter.getFeedFile().get(0).getFileLength()
							+ ")");
					if (type.equals("jpg") || type.equals("JPG")
							|| type.equals("png") || type.equals("PNG")
							|| type.equals("jpeg") || type.equals("bmp")
							|| type.equals("gif")) {
						holder.downloadpic.setVisibility(View.GONE);// 图片的话，把我要下载隐藏
						holder.downwenjian.setVisibility(View.GONE);
						holder.gv.setVisibility(View.VISIBLE);

						Type type2 = new TypeToken<List<com.cloudcc.cloudframe.model.DynamicModel.FeedFile>>() {
						}.getType();
						List<com.cloudcc.cloudframe.model.DynamicModel.FeedFile> list2 = GsonUtil
								.covertList(l, type2);
						IndexGridViewAdapter adapter = new IndexGridViewAdapter(
								context, list2);
						holder.gv.setAdapter(adapter);
					} else {
						holder.gv.setVisibility(View.GONE);
						holder.downloadpic.setVisibility(View.VISIBLE);// 文件的话，我要下载显示
						holder.downwenjian.setVisibility(View.VISIBLE);
					}
				}
			}
		} else {
			holder.aboutpic.setVisibility(View.GONE);
		}
		holder.delete.setVisibility(View.VISIBLE);
		if (!myChatter.getAuthorId().equals(
				UserManager.getManager().getUser().userId)) {
			holder.delete.setVisibility(View.GONE);
		}
		holder.xihuan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.xihuan(position, holder.xihuan);
			}
		});
		holder.shoucang.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.shoucang(position);
			}
		});
		holder.shanchuaa.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.shanchu(position);
			}
		});
		holder.pinglun.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.pinglun(position);
			}
		});
		holder.downloadpic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.downloadpic(position);
			}
		});
		holder.icon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//点击头像
				onItemListener.icon(position);
			}
		});
		holder.nicheng.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.nicheng(position);
			}
		});
		holder.bttoupiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.btntoupiao(position);
			}
		});
		holder.zhengti.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onItemListener.zhengti(position);
			}
		});
		holder.target.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AppContext.urlString = UrlTools.weixinUrl
						+ UrlTools.getObjectUrl(myChatter.getTargetId());
				Intent intent = new Intent(context, RelevantActivtiy.class);
				intent.putExtra("web", myChatter.getTargetId());
				context.startActivity(intent);
			}
		});
		holder.event.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AppContext.urlString = UrlTools.weixinUrl
						+ UrlTools.getTaskOrEventUrl(myChatter
								.getTaskIdOrEventId());
				Intent intent = new Intent(context, RelevantActivtiy.class);
				intent.putExtra("web", myChatter.getTaskIdOrEventId());
				context.startActivity(intent);
			}
		});

		return convertView;
	}

	private void isContiaTarget(final ViewHolder holder,
			final MyChatter myChatter) {
		if ("en".equals(mEns)) {

			
			if (myChatter.getTargetId() != null) {
				holder.targetIdname.setVisibility(View.VISIBLE);
				holder.targetIdlx.setVisibility(View.VISIBLE);
				holder.target.setVisibility(View.VISIBLE);
				holder.targetIdlx.setText("Association →" + "recording");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
				holder.targetimg.setBackgroundResource(R.drawable.linked);
			} else {
				noGtype(holder);
			}
			if (myChatter.getTaskIdOrEventId() != null
					&& !myChatter.getTaskIdOrEventId().equals("")) {
				holder.eventIdname.setVisibility(View.VISIBLE);
				holder.event.setVisibility(View.VISIBLE);
				holder.below_address.setVisibility(View.VISIBLE);
				if (myChatter.getTaskOrEventName() != null
						&& !myChatter.getTaskOrEventName().equals("")) {
					if (myChatter.getTaskOrEventName().equals("Record the phone")) {
						holder.eventIdname.setText("Activity: Phone");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_dianhua_h);
					} else if (myChatter.getTaskOrEventName().equals("Record SMS")) {
						holder.eventIdname.setText("Activity: SMS");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_duanxin_h);
					} else if (myChatter.getTaskOrEventName().equals("Sign in to visit")) {
						holder.eventIdname.setText("Activity: sign in to visit");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_qdbf_h);
					} else if (myChatter.getTaskOrEventName().equals("Record mail")) {
						holder.eventIdname.setText("Activity: Mail");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_youjian_h);
					} else if (myChatter.getTaskOrEventName().equals("Write a note")) {
						holder.eventIdname.setText("Activity: Remarks");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_beizhu_h);
					} else if (myChatter.getTaskOrEventName().equals("Report the record")) {
						holder.eventIdname.setText("Activity: Reporting");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_hbjl_h);
					} else {
						holder.eventIdname.setText("Activity:"
								+ myChatter.getTaskOrEventName());
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_qdbf_h);
					}
				}
			} else {
				noEvent(holder);
			}
		
//		------------------------------------------------------------	
		}else{

			
			if (myChatter.getTargetId() != null) {
				holder.targetIdname.setVisibility(View.VISIBLE);
				holder.targetIdlx.setVisibility(View.VISIBLE);
				holder.target.setVisibility(View.VISIBLE);
				holder.targetIdlx.setText("关联 →" + "记录");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
				holder.targetimg.setBackgroundResource(R.drawable.linked);
			} else {
				noGtype(holder);
			}
			if (myChatter.getTaskIdOrEventId() != null
					&& !myChatter.getTaskIdOrEventId().equals("")) {
				holder.eventIdname.setVisibility(View.VISIBLE);
				holder.event.setVisibility(View.VISIBLE);
				holder.below_address.setVisibility(View.VISIBLE);
				if (myChatter.getTaskOrEventName() != null
						&& !myChatter.getTaskOrEventName().equals("")) {
					if (myChatter.getTaskOrEventName().equals("记录电话")) {
						holder.eventIdname.setText("活动:电话");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_dianhua_h);
					} else if (myChatter.getTaskOrEventName().equals("记录短信")) {
						holder.eventIdname.setText("活动:短信");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_duanxin_h);
					} else if (myChatter.getTaskOrEventName().equals("签到拜访")) {
						holder.eventIdname.setText("活动:签到拜访");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_qdbf_h);
					} else if (myChatter.getTaskOrEventName().equals("记录邮件")) {
						holder.eventIdname.setText("活动:邮件");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_youjian_h);
					} else if (myChatter.getTaskOrEventName().equals("写个备注")) {
						holder.eventIdname.setText("活动:备注");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_beizhu_h);
					} else if (myChatter.getTaskOrEventName().equals("汇报记录")) {
						holder.eventIdname.setText("活动:汇报记录");
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_hbjl_h);
					} else {
						holder.eventIdname.setText("活动:"
								+ myChatter.getTaskOrEventName());
						holder.eventimg
								.setBackgroundResource(R.drawable.qiandao_qdbf_h);
					}
				}
			} else {
				noEvent(holder);
			}
		
		}
	}

	private void setTargetIdName(final ViewHolder holder,
			final MyChatter myChatter) {
		if ("en".equals(mEns)) {

			if (myChatter.getTargetId().substring(0, 3).equals("001")) {
				holder.targetIdlx.setText("Association→" + "client");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			} else if (myChatter.getTargetId().substring(0, 3).equals("003")) {
				holder.targetIdlx.setText("Association →" + "Contact person");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			} else if (myChatter.getTargetId().substring(0, 3).equals("004")) {
				holder.targetIdlx.setText("Association →" + "Potential customers");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			} else {
				holder.targetIdlx.setText("");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			}
		
//			--------------------------
		}else{

			if (myChatter.getTargetId().substring(0, 3).equals("001")) {
				holder.targetIdlx.setText("关联 →" + "客户");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			} else if (myChatter.getTargetId().substring(0, 3).equals("003")) {
				holder.targetIdlx.setText("关联 →" + "联系人");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			} else if (myChatter.getTargetId().substring(0, 3).equals("004")) {
				holder.targetIdlx.setText("关联 →" + "潜在客户");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			} else {
				holder.targetIdlx.setText("");
				holder.targetIdname
						.setText("【" + myChatter.getTargetIdname() + "】");
			}
		
		}
	}

	private void noGtype(final ViewHolder holder) {
		holder.targetIdname.setVisibility(View.GONE);
		holder.target.setVisibility(View.GONE);
		holder.targetIdlx.setVisibility(View.GONE);
	}

	private void noEvent(final ViewHolder holder) {
		holder.below_address.setVisibility(View.GONE);
		holder.eventIdname.setVisibility(View.GONE);
		holder.event.setVisibility(View.GONE);
	}

	class ViewHolder {
		private ImageView icon;
		private TextView nicheng, mydownload, counttp, targetIdlx,
				below_address;
		private Button bttoupiao;
		private TextView time, address;
		private TextView wenben;
		private ImageView image;
		private TextView lianjiename, targetIdname, eventIdname;
		private ImageView lianjieimg;
		private TextView countly, countxh;
		private ImageView favourite, praised, delete, comment, download,
				targetimg, eventimg;
		private RelativeLayout shoucang, xihuan, shanchuaa, pinglun;
		private GridViewForListView gv;
		private RelativeLayout lianjieurl, downloadpic, aboutpic, zhengti,
				aboutaddress;
		private TextView downwenjian;
		private LinearLayout abouttoupiao, abcapoll, target, event;
	}

	private void setBody(String bodyText, TextView tvBody) {
		int a = 0;
		int b = 0;
		// 这里没有考虑body长度大于100的情况，你自己考虑一下吧
		if (bodyText == null) {
			return;
		}
		tvBody.setMovementMethod(LinkMovementMethod.getInstance());

		String pattern = "<a[^>]*>([^<]*)</a>";
		Matcher matcher = Pattern.compile(pattern).matcher(bodyText);

		int lastSpanEndIndex = 0; // 每次匹配成功的字符串的最后索引
		SpannableString spannableStr = null; // "@XXX"
		String aLabelString = null; // a标签，形如"<a href="xxx">fdsafd</a>"

		String Id = null;
		while (matcher.find()) {
			a++;
			int index = matcher.start();

			aLabelString = matcher.group();

			tvBody.append(Html.fromHtml(bodyText.substring(lastSpanEndIndex,
					index)));
			b = index;
			String userIdTemp = aLabelString.substring(aLabelString
					.indexOf("&userid="));
			Id = userIdTemp.substring(userIdTemp.indexOf("=") + 1,
					userIdTemp.indexOf("'"));

			ChatterClickableSpan chatterClickableSpan = new ChatterClickableSpan(
					Id, context);

			spannableStr = new SpannableString(Html.fromHtml(aLabelString)
					.toString());

			spannableStr.setSpan(chatterClickableSpan, 0,
					spannableStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

			tvBody.append(spannableStr);

			lastSpanEndIndex = matcher.end();

		}
		if (a == 0) {
			tvBody.setText(Html.fromHtml(bodyText));
		} else {
			if (lastSpanEndIndex < bodyText.length()) {
				tvBody.append(Html.fromHtml(bodyText.substring(
						lastSpanEndIndex, bodyText.length())));
			}
		}
	}

	// 人名单击的监听
	public static class ChatterClickableSpan extends ClickableSpan {
		String iId;
		Context context;

		public ChatterClickableSpan(String userId, Context context) {
			super();
			this.iId = userId;
			this.context = context;
		}

		@Override
		public void onClick(View widget) {
			Intent intent = new Intent(context, NewUserInfoActivity.class);
			intent.putExtra("userId", iId);
			context.startActivity(intent);
		}

	}

	public interface OnItemListener {
		public void nicheng(int position);

		public void icon(int position);

		public void pinglun(int position);// 评论

		public void xihuan(int position, View v);// 赞

		public void shoucang(int position);// 收藏

		public void shanchu(int position);// 删除

		public void downloadpic(int position);// 如果是文件下载文件

		public void btntoupiao(int position);//

		public void zhengti(int position);// 整体监听
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {
		private String id;
		private String path;

		public AnimateFirstDisplayListener(String imageId, String path) {
			this.id = imageId;
			this.path = path;
		}

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
					ImageUtils.saveImage(path, id, loadedImage);
				}
			}
		}
	}

	/**
	 * 判别是否存在
	 * 
	 * @return
	 * @throws DbException
	 */
	public boolean isExit(DbUtils db) throws DbException {
		boolean istable = db.tableIsExist(FeedFile.class);
		if (istable) {
			long a = db.count(FeedFile.class);
			if (a > 0) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
}
